/**
 * 联众图像识别函数
 * @param {string} username 联众图像识别账号 
 * @param {string} password 联众图像识别密码
 * @param {object} img 识别图片
 * 请求参数说明:
softwareId：软件ID，⽤于标识软件的身份，软件作者登录联众识别平台后显示。软件ID及
softwareSecret应当由软件开发者写⼊到软件内部，不允许普通⽤户修改。软件开发者在联众⽹站
⽤户中⼼获取。
softwareSecret：接⼊联众识别的软件密钥，应当由软件开发者写⼊到软件内部，不允许普通⽤户
修改。软件开发者在联众⽹站⽤户中⼼获取。
username：联众⽤户名
password：根据⽤户在联众⽤户中⼼的设置，将联众⽤户的 密码 或 识别独⽴密码 传输给服务
器。
captchaData：图⽚⽂件内容，base64后的数据
captchaType：识别类型(整形)，由联众平台分配，请查阅联众⽹站价格⻚⾯
captchaMinLength：可选参数，识别时需输⼊的最⼩⻓度。注，1个中⽂汉字 或 1个英⽂字⺟都
记为⻓度 1。例如：联众识别 记 4，abcd 记 4
captchaMaxLength：可选参数，识别时最多输⼊的内容⻓度。计数规则同上
workerTipsId: 可选参数，⼈⼯提示模板ID
 */
 function getCode(username, password, img) {
    http.__okhttp__.setTimeout(3e4);
    var r = images.toBase64(img, format = "png"), i = device.release, c = device.model, s = device.buildId;
    try {
        var n = http.postJson("https://v2-api.jsdama.com/upload", {
            softwareId: 25212,//软件ID
            softwareSecret: "yw6VTrAqJ5lHhJZPCGUh8CQvg9imK2kbmz9gEt66",//秘钥
            username: username,
            password: password,
            captchaData: r,
            captchaType: 1001,//识别类型(整形)
            captchaMinLength: 0,
            captchaMaxLength: 0,
            workerTipsId: 0
        }, {
            headers: {
                "User-Agent": "Mozilla/5.0 (Linux; Android " + i + "; " + c + " Build/" + s + "; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/66.0.3359.126 Mobile Safari/537.36",
            }
        });
    } catch (e) {
        return {
            code: "-1",
            msg: "网络链接超时...",
            data: {}
        };
    }
    var d = n.body.json(), p = d.code, m = d.message;
    if ("10079009" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142006" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142004" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10142005" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("10079006" == p) return {
        code: p,
        msg: m,
        data: {}
    };
    if ("0" == p) {
        return {
            code: p,
            msg: m,
            data: {
                res: d.data.recognition,
                ID: d.data.captchaId
            }
        };
    }
    return d;
}
/**
 * 获得用户点数函数
 * @param {string} username 联众图像识别账号 
 * @param {string} password 联众图像识别密码
 */
function getdianshu(username, password) {
    http.__okhttp__.setTimeout(3e4);
    try {
        var n = http.postJson("https://v2-api.jsdama.com/check-points", {
            "softwareId":25212,//软件ID
            "softwareSecret":"yw6VTrAqJ5lHhJZPCGUh8CQvg9imK2kbmz9gEt66",//秘钥
            "username":username,
            "password":password
        }
        );
    } catch (e) {
        return {
            code: "-1",
            msg: "网络链接超时...",
            data: {}
        };
    }
    var d = n.body.json();
    return d;
}
/**
 * 报错函数
 * @param {string} username 联众图像识别账号 
 * @param {string} password 联众图像识别密码
 */
 function baocuo(username, password, ID) {
    http.__okhttp__.setTimeout(3e4);
    try {
        var n = http.postJson("https://v2-api.jsdama.com/report-error", {
            "softwareId":25212,//软件ID
            "softwareSecret":"yw6VTrAqJ5lHhJZPCGUh8CQvg9imK2kbmz9gEt66",//秘钥
            "username":username,
            "password":password,
            "captchaId":ID//：由 接⼝1 返回的 captchaId 识别ID

        }
        );
    } catch (e) {
        return {
            code: "-1",
            msg: "网络链接超时...",
            data: {}
        };
    }
    var d = n.body.json();
    return d;
}



path="/sdcard/aa/13.png"
var src = images.read(path);
var imgScreen = images.clip(src, 100, 1100, 600, 200);
images.save(imgScreen, "/sdcard/aa/313.png");
sleep(1000) 
path= "/sdcard/aa/313.png"
username="tfnmdmx"
password="qyl505443211."
img=images.read(path)
sleep(1000)
log("识别结果返回：")
result=getCode(username, password, img)
log(result)
log("验证码：")
log(result.data.res)
log("ID")
log(result.data.ID)
sleep(1000)
log("用户点数返回：")
dianshu=getdianshu(username, password)
log(dianshu)
log("用户可用点数：")
log(dianshu.data.availablePoints)



/*
if()//一定要有判断，比如输入后显示验证码错误，进行截图判断
{
    baocuo_result = baocuo(username, password, result.data.ID)
    if(baocuo_result.data.result == true){
        log("报错成功")
    }
    else{
        log("报错失败")
    }
}
*/